package com.zl.test.restdemo.common.utils;

import com.zl.test.restdemo.exception.DemoServiceException;
import lombok.Data;

/**
 * @Author: zhang lei
 * @Describe: All things are difficult before they are easy.
 * @CreatTime: 2021-02-24-16-37
 */
@Data
public class ResponseEntity<T> {
    T data;
    String message;
    boolean success;
    int errCode=0;

    public static final String OK = "ok";
    public static final String NG = "error";
    public static <T> ResponseEntity<T> success() {
        return new ResponseEntity<>(true, OK);
    }

    public static <T> ResponseEntity<T> success(T data) {
        return new ResponseEntity<>(data, true, OK);
    }
    public static <T> ResponseEntity<T> failure(String message){
        return new ResponseEntity<>(false, message,1);
    }

    public static <T> ResponseEntity<T> failure(){
        return new ResponseEntity<>(false, NG,1);
    }

    public static <T> ResponseEntity<T> failure(String message, int errCode){
        return new ResponseEntity<>(false, message,errCode);
    }

    public static <T> ResponseEntity<T> failure(String message, int errCode, T data){
        return new ResponseEntity<T>(false, message,errCode, data);
    }

    public static <T> ResponseEntity<T> failure(DemoServiceException e){
        ResponseEntity objectResponseEntity = new ResponseEntity(false, e.getMessage(), Integer.valueOf(e.getCode()));
        if (e.getData() != null) {
            objectResponseEntity.setData( e.getData());
        }
        return objectResponseEntity;
    }

    public static <T> ResponseEntity<T> success(String message, int errCode) {
        return new ResponseEntity<>(true, message, errCode);
    }

    private ResponseEntity(){}

    private ResponseEntity(T data){
        this.data = data;
    }

    private ResponseEntity(T data, boolean success){
        this(data);
        this.success = success;
    }

    public boolean getSuccess()
    {
        return this.success;
    }

    public boolean isSuccess()
    {
        return this.success;
    }


    private ResponseEntity(boolean success, String message){
        this.success = success;
        this.message = message;
    }
    private ResponseEntity(boolean success, String message, int errCode){
        this.success = success;
        this.message = message;
        this.errCode = errCode;
    }

    private ResponseEntity(boolean success, String message, int errCode, T data){
        this(success, message, errCode);
        this.data = data;
    }

    private ResponseEntity(T data, boolean success, String message){
        this(data, success);
        this.message = message;
    }

}
